Welcome To
String Format


String formatting or Output Format

The string Formatting is a method to display the variable value inside string. the python provides the four ways to forming the string . that shown in the below .the string formatting is a way to displaying string inside variable value.

Forming Ways

  1. String format using % operator.
  2. String format using str.format()
  3. String format using f-string
  4. String format using template class

This are the ways to forming the string in side the variable values

String Format Using % operator

it is a old way to forming the string in the variable value . like in c programming when we are using %d to format the sting same way but some difference. Examples shown in the below

Examples

name="darshan"
    print('The %s is a web developer')
Output:- The darshan is a web developer

age=19
   print( 'The developer age is %d '%(age))
Oputput:- The devloper age is 19

salary=20000.00
   print("The developer salary is %f"%(salary))
Output:- The developer salary is 20000.000

String Format Using str.Format()

The string Format is a process of displaying the variable value inside the string by using the .Format() method . there is a two parts position and keyword
Position means index value place of the variable can displaying inside the curly braces{0}. keyword is a value give in the same line Example in the below

Examples

name='hemanth'
age=20
  print('The {0} is {1} years old'.format(name,age))

print('The {name} is{age} years old'.format(name="Tharun",20))
Output:- The Tharun is 20 years old

String Format Using f-string

This method is latest version 3.6 and simple way to forming the variable value inside the string Examples in the below and the variable name inside the string with curly braces

Examples

name="Nandini"
print(f'The{name} is a good friend of me )
output:-The Nandini is a good friend of me

String format using Template class

this is useful for expert programmers . import some class and using the value. no examples